package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.User;
import com.service.impl.UserServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主要用于管理员在后台修改用户个人的部分信息
 * @author shkstart
 * @create 2022-06-19-16:15
 */
@RestController
@RequestMapping("/manage")
public class UserManageController {
    @Resource
    UserServiceImpl userService;


    //分页+模糊查找用户

    @GetMapping("/userPage")
    public IPage<User> goodsPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String nickName,//前端不传该参数时 默认为""
                                 @RequestParam(defaultValue = "") String phone,
                                 @RequestParam(defaultValue = "") String userRole,
                                 @RequestParam(defaultValue = "") String address){
        IPage<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //当数据库中该数据为null也要模糊匹配查询
        queryWrapper.like(!Strings.isEmpty(nickName),"nick_name",nickName);
        queryWrapper.like(!Strings.isEmpty(phone),"phone",phone);
        queryWrapper.like(!Strings.isEmpty(userRole),"user_role",userRole);
        queryWrapper.like(!Strings.isEmpty(address),"address",address);
        //        根据id倒叙排列
        queryWrapper.orderByDesc("user_id");
        IPage<User> userPage = userService.page(page, queryWrapper);
        return userPage;
    }

    //根据id删除指定的用户
    @DeleteMapping("/deleteUser/{id}")
    public boolean deleteUser(@PathVariable Integer id){
        return userService.removeById(id);
    }

    //实现对商品的批量删除
    @PostMapping("/delUser/batch")
    public boolean delGoodsBatch(@RequestBody List<Integer> ids){
        return userService.removeByIds(ids);
    }

    //冻结用户
    @GetMapping("/freeze")
    public boolean doFreeze(@RequestParam Integer userId){
        User user = userService.getById(userId);
        user.setStatus(0);
        return userService.updateById(user);
    }

    //激活用户
    @GetMapping("/activate")
    public boolean doActivate(@RequestParam Integer userId){
        User user = userService.getById(userId);
        user.setStatus(1);
        return userService.updateById(user);
    }

}
